package com.deyuanyun.pic.integalz.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.deyuanyun.pic.business.controller.dto.PipeLineNewTaskVO;
import org.springframework.stereotype.Service;

import com.deyuanyun.pic.integalz.controller.dto.IntegalzInspTaskQueryVO;
import com.deyuanyun.pic.integalz.enumeration.InspTableNameEnum;
import com.deyuanyun.pic.integalz.mapper.IntegalzInspTaskMapper;
import com.deyuanyun.pic.integalz.service.PipeLineInspTaskService;
import com.deyuanyun.pic.pipe.domain.PipeSegmentInfo;
import com.deyuanyun.pic.pipe.domain.PipeSegmentInfoExample;
import com.deyuanyun.pic.pipe.mapper.PipeSegmentInfoMapper;
import com.deyuanyun.pic.settings.domain.pipe.PipeSegment;
import com.deyuanyun.pic.settings.mapper.pipe.PipeSegmentMapper;
import com.deyuanyun.pic.settings.support.biz.CodeUtil;

@Service
public class PipeLineInspTaskServiceImpl implements PipeLineInspTaskService {
    
    private @Resource IntegalzInspTaskMapper integalzInspTaskMapper;
    private @Resource PipeSegmentMapper pipeSegmentMapper;
    private @Resource PipeSegmentInfoMapper pipeSegmentInfoMapper;
    
    @Override
    public List<PipeLineNewTaskVO> queryNewInspTaskMerge(String pipeLineId) {
        
        List<PipeLineNewTaskVO> pipeLineInspTaskList = new ArrayList<PipeLineNewTaskVO>();
        
        List<Map<String,Object>> list = integalzInspTaskMapper.findInspTask(pipeLineId,null,true);
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = list.get(i);
            
            BigDecimal beginMileage = (BigDecimal) map.get("STARTMILEAGE");
            BigDecimal endMileage = (BigDecimal) map.get("ENDMILEAGE");
            String id = (String)map.get("ID");
            Date completionDate = (Date)map.get("PAG_DATETIME");
            
            Boolean includeBegin = null;
            
            //第一段100%需要完整加入
            if(pipeLineInspTaskList.isEmpty()){
                PipeLineNewTaskVO pipeLineInspTask_1 = new PipeLineNewTaskVO();
                pipeLineInspTask_1.setStartMileage(beginMileage);
                pipeLineInspTask_1.setEndMileage(endMileage);
                pipeLineInspTask_1.setId(id);
                pipeLineInspTask_1.setCompletionDate(completionDate);
                
                pipeLineInspTask_1.setIncludeStart(true);
                pipeLineInspTask_1.setIncludeEnd(true);
                pipeLineInspTaskList.add(pipeLineInspTask_1);
                continue;
            }
            
            //循环对比
            for (int j = 0; j < pipeLineInspTaskList.size(); j++) {
                PipeLineNewTaskVO pipeLineInspTask = new PipeLineNewTaskVO();
                pipeLineInspTask.setStartMileage(beginMileage);
                pipeLineInspTask.setEndMileage(endMileage);
                pipeLineInspTask.setId(id);
                pipeLineInspTask.setCompletionDate(completionDate);
                
                PipeLineNewTaskVO pipeLineInspTask_T = pipeLineInspTaskList.get(j);
                BigDecimal beginMileage_T = pipeLineInspTask_T.getStartMileage();
                BigDecimal endMileage_T = pipeLineInspTask_T.getEndMileage();
                //String id_T = pipeLineInspTask_T.getId();
                if(endMileage.compareTo(beginMileage_T) == -1){
                    //在前面，终点比起点小
                    pipeLineInspTask.setIncludeStart(includeBegin == null ? true : includeBegin);
                    pipeLineInspTask.setIncludeEnd(true);
                    pipeLineInspTaskList.add(j++, pipeLineInspTask);//加在当前元素的前面
                    break;
                }
                
                if(beginMileage.compareTo(beginMileage_T) == -1 && endMileage.compareTo(beginMileage_T) > -1){
                    //有交叉，起点比起点小，终点比起点大或者等于。
                    pipeLineInspTask.setIncludeStart(includeBegin == null ? true : includeBegin);
                    pipeLineInspTask.setIncludeEnd(false);
                    pipeLineInspTask.setEndMileage(beginMileage_T);
                    pipeLineInspTaskList.add(j++, pipeLineInspTask);//加在当前元素的前面
                    beginMileage = beginMileage_T;//切断。
                }

                if(beginMileage.compareTo(endMileage_T) < 1 && endMileage.compareTo(endMileage_T) == 1){
                    //有交叉，起点比终点小或者等于，终点比终点大。
                    if(j == pipeLineInspTaskList.size() - 1){//只有处于最后一个了，才往里面加。
                        pipeLineInspTask.setIncludeStart(includeBegin == null ? false : includeBegin);
                        pipeLineInspTask.setIncludeEnd(true);
                        pipeLineInspTask.setStartMileage(endMileage_T);
                        pipeLineInspTaskList.add(++j, pipeLineInspTask);//加在当前元素的后面
                    }
                    beginMileage = endMileage_T;//切断。
                    includeBegin = false;//不能包含起点了。
                }
                if(beginMileage.compareTo(endMileage_T) == 1 && j == pipeLineInspTaskList.size() - 1){
                    //在后面，起点比终点大，并且处于最后一个了。
                    pipeLineInspTask.setIncludeStart(includeBegin == null ? true : includeBegin);
                    pipeLineInspTask.setIncludeEnd(true);
                    pipeLineInspTaskList.add(++j, pipeLineInspTask);//加在当前元素的后面
                    //break;
                }
            }
            
        }
        
        return pipeLineInspTaskList;
    }

    
    @Override
    public List<PipeLineNewTaskVO> qeurySecondInspTaskMerge(String pipeLineId,List<PipeLineNewTaskVO> pipeLineNewTaskList1) {
        
        List<PipeLineNewTaskVO> pipeLineNewTaskList2 = new ArrayList<PipeLineNewTaskVO>(pipeLineNewTaskList1.size());
        for (int i = 0; i < pipeLineNewTaskList1.size(); i++) {
            
            PipeLineNewTaskVO pipeLineNewTask = pipeLineNewTaskList1.get(i);
            List<Map<String,Object>> list = integalzInspTaskMapper.findInspTask(pipeLineId, pipeLineNewTask,true);//取上一条任务
            if(list != null && !list.isEmpty() ){
                Map<String,Object> map = list.get(0);
                BigDecimal beginMileage = (BigDecimal) map.get("STARTMILEAGE");
                BigDecimal endMileage = (BigDecimal) map.get("ENDMILEAGE");
                String id = (String)map.get("ID");
                Date completionDate = (Date)map.get("PAG_DATETIME");

                PipeLineNewTaskVO pipeLineInspTask_1 = new PipeLineNewTaskVO();
                pipeLineInspTask_1.setStartMileage(beginMileage);
                pipeLineInspTask_1.setEndMileage(endMileage);
                pipeLineInspTask_1.setId(id);
                pipeLineInspTask_1.setCompletionDate(completionDate);
                
                pipeLineInspTask_1.setIncludeStart(pipeLineNewTask.getIncludeStart());
                pipeLineInspTask_1.setIncludeEnd(pipeLineNewTask.getIncludeEnd());
                pipeLineNewTaskList2.add(pipeLineInspTask_1);
            }else{
                pipeLineNewTaskList2.add(null);//为了对比任务，固加此语句。
            }
        }
        
        return pipeLineNewTaskList2;
    }





    @Override
    public List<Map<String, Object>> queryInspMetaLoss(IntegalzInspTaskQueryVO integalzQuery,List<PipeLineNewTaskVO> pipeLineInspTaskList) {
        String featureType = CodeUtil.getCodeId("METALOSS_TYPE", "内部金属损失");
        for (int i = 0; i < pipeLineInspTaskList.size(); i++) {
            PipeLineNewTaskVO pipeLineNewTask = pipeLineInspTaskList.get(i);
            if(pipeLineNewTask == null){//如果有null元素，移除。
                pipeLineInspTaskList.remove(i--);
            }
        }
        List<Map<String, Object>> list = integalzInspTaskMapper.queryMetaLoss(featureType,integalzQuery, pipeLineInspTaskList);
        
        return list;
    }

    @Override
    public List<Map<String, Object>> queryExinspMetaLoss(IntegalzInspTaskQueryVO integalzQuery,List<PipeLineNewTaskVO> pipeLineInspTaskList) {
        
        String featureType = CodeUtil.getCodeId("METALOSS_TYPE", "外部金属损失");
        for (int i = 0; i < pipeLineInspTaskList.size(); i++) {
            PipeLineNewTaskVO pipeLineNewTask = pipeLineInspTaskList.get(i);
            if(pipeLineNewTask == null){//如果有null元素，移除。
                pipeLineInspTaskList.remove(i--);
            }
        }
        List<Map<String, Object>> list = integalzInspTaskMapper.queryMetaLoss(featureType,integalzQuery, pipeLineInspTaskList);
        return list;
    }

    @Override
    public List<Map<String, Object>> queryGirthWeld(IntegalzInspTaskQueryVO integalzQuery
            ,List<PipeLineNewTaskVO> pipeLineInspTaskList) {
        
        List<Map<String,Object>> list = integalzInspTaskMapper.queryGirthWeld(integalzQuery, pipeLineInspTaskList);
        
        return list;
    }

    @Override
    public List<Map<String, Object>> queryGwwRepairind(IntegalzInspTaskQueryVO integalzQuery) {
        List<Map<String,Object>> list = integalzInspTaskMapper.queryGwwRepairind(integalzQuery);
        
        return list;
    }


    @Override
    public List<Map<String, Object>> queryInspDefect(InspTableNameEnum inspTableNameEnum, String inspTaskId,BigDecimal startMileage,BigDecimal endMileage) {
        
        //String tableName = inspTableNameEnum.getLabel();
        List<Map<String, Object>> list = integalzInspTaskMapper.queryInspDefect(inspTableNameEnum, inspTaskId,startMileage,endMileage);
        return list;
    }


    @Override
    public List<Map<String, Object>> findInspTask(String pipeLineId,PipeLineNewTaskVO pipeLineNewTask,Boolean detectorTypeIsnull) {
        
        List<Map<String, Object>> list = integalzInspTaskMapper.findInspTask(pipeLineId, pipeLineNewTask,detectorTypeIsnull);
        return list;
    }


    @Override
    public PipeSegmentInfo querySegmentInfo(String pipeLineId,BigDecimal mileage) {
        
        PipeSegment pipeSegment = pipeSegmentMapper.queryPipeSegmentByMileage(pipeLineId, mileage);
        
        PipeSegmentInfoExample example = new PipeSegmentInfoExample();
        example.createCriteria().andPipeSegmentIdEqualTo(pipeSegment.getId()).andDeletedEqualTo((short)0);
        List<PipeSegmentInfo> pipeSegmentInfoList = pipeSegmentInfoMapper.selectByExample(example);
        if(pipeSegmentInfoList != null && !pipeSegmentInfoList.isEmpty()){
            return pipeSegmentInfoList.get(0);
        }
        return null;
    }
    
    
    
}
